//
//  CompleteVipCardInfoView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/31.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "CompleteVipCardInfoView.h"
#import "MOFSPickerManager.h"

@implementation CompleteVipCardInfoView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
     
        self.backgroundColor = BackGroundColor;
        
        self.bgNameArray = [NSMutableArray arrayWithObjects:@"currencycard_card_black",@"currencycard_card_white",@"currencycard_card_yellow", nil];
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        UIView *whiteBgView = [[UIView alloc] init];
        whiteBgView.backgroundColor = CSWhiteColor;
        [scrollView addSubview:whiteBgView];
        self.whiteBgView = whiteBgView;
        
        /**
         卡面背景
         */
        UIImageView *bgView = [[UIImageView alloc] init];
        bgView.layer.cornerRadius = 4;
        bgView.layer.masksToBounds = YES;
        [whiteBgView addSubview:bgView];
        self.bgView = bgView;
        
        /**
         卡名称
         */
        UILabel *cardClassNameLabel = [[UILabel alloc] init];
        cardClassNameLabel.font = BoldBigFont;
        cardClassNameLabel.text = @"次卡类型(46)";
        cardClassNameLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:cardClassNameLabel];
        self.cardClassNameLabel = cardClassNameLabel;
        
        /**
         有效期
         */
        UILabel *validLabel = [[UILabel alloc] init];
        validLabel.font = MidFont;
        validLabel.text = @"有效期:2017-10-01至2018-10-01";
        validLabel.textColor = CSColorFromRGB(0x97969c);
        [bgView addSubview:validLabel];
        self.validLabel = validLabel;
        
        /**
         会员ID
         */
        UILabel *vipNameLabel = [[UILabel alloc] init];
        vipNameLabel.font = MidBigFont;
        vipNameLabel.text = @"疯狂的兔子";
        vipNameLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:vipNameLabel];
        self.vipNameLabel = vipNameLabel;
        
        /**
         会员卡号
         */
        TTTAttributedLabel *cardNumLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        cardNumLabel.font = SmallFont;
        cardNumLabel.textColor = CSColorFromRGB(0x333333);
        cardNumLabel.lineBreakMode = NSLineBreakByCharWrapping;
        cardNumLabel.numberOfLines = 0;
        cardNumLabel.lineSpacing = 5;
        [bgView addSubview:cardNumLabel];
        self.cardNumLabel = cardNumLabel;

        
        /**
         余额
         */
        UILabel *balanceLabel = [[UILabel alloc] init];
        balanceLabel.font = SmallFont;
        balanceLabel.text = @"余额";
        balanceLabel.textColor = CSColorFromRGB(0x97969c);
        [bgView addSubview:balanceLabel];
        self.balanceLabel = balanceLabel;
        
        
        /**
         余额
         */
        UILabel *balanceDetailLabel = [[UILabel alloc] init];
        balanceDetailLabel.font = MidBigFont;
        balanceDetailLabel.text = @"余额";
        balanceDetailLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:balanceDetailLabel];
        self.balanceDetailLabel = balanceDetailLabel;
        
        /**
         销售
         */
        CommonBtnView *salBtnView = [[CommonBtnView alloc] init];
        salBtnView.titleLabel.text = @"销售*";
        salBtnView.choseLabel.text = @"请选择";
        salBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:salBtnView];
        self.salBtnView = salBtnView;

        
        /**
         会员卡号
         */
        CommonInputView *vipCardInputView = [[CommonInputView alloc] init];
        vipCardInputView.titleLabel.text = @"会员卡号*";
        [scrollView addSubview:vipCardInputView];
        self.vipCardInputView = vipCardInputView;
        
        /**
         介绍人
         */
        CommonInputView *introduceBtnView = [[CommonInputView alloc] init];
        introduceBtnView.titleLabel.text = @"介绍人";
        introduceBtnView.titleTF.placeholder = @"选填";
        [scrollView addSubview:introduceBtnView];
        self.introduceBtnView = introduceBtnView;
        
        /**
         对接教练
         */
        CommonBtnView *coachBtnView = [[CommonBtnView alloc] init];
        coachBtnView.titleLabel.text = @"对接教练*";
        coachBtnView.choseLabel.text = @"请选择";
        coachBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:coachBtnView];
        self.coachBtnView = coachBtnView;
        
        
        /**
         备注
         */
        CommonInputView *remarkBtnView = [[CommonInputView alloc] init];
        remarkBtnView.titleLabel.text = @"备注";
        remarkBtnView.titleTF.placeholder = @"请输入备注，选填";
        [scrollView addSubview:remarkBtnView];
        self.remarkBtnView = remarkBtnView;

        /**
         支付
         */
        CommonBtnView *payBtnView = [[CommonBtnView alloc] init];
        payBtnView.titleLabel.text = @"支付";
        payBtnView.choseLabel.text = @"选择支付方式";
        payBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:payBtnView];
        self.payBtnView = payBtnView;
        
        
        
        /**
         历史订单
         */
        CommentSwitchView *hisSwitchView = [[CommentSwitchView alloc] init];
        hisSwitchView.titleLabel.text = @"历史订单";
        [hisSwitchView.titleSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        [scrollView addSubview:hisSwitchView];
        self.hisSwitchView = hisSwitchView;


        TTTAttributedLabel *hisLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        hisLabel.font = SmallFont;
        hisLabel.textColor = CSColorFromRGB(0x7a7a7a);
        hisLabel.numberOfLines = 0;
        hisLabel.lineBreakMode = NSLineBreakByCharWrapping;
        hisLabel.text = @"开启【历史订单】功能后，可以补录实际年月份的订单记录。";
        [scrollView addSubview:hisLabel];
        self.hisLabel = hisLabel;


        UIView *bottomView = [[UIView alloc] init];
        bottomView.backgroundColor = CSWhiteColor;
        [self addSubview:bottomView];
        self.bottomView = bottomView;
        
        UILabel *shiShouLabel = [[UILabel alloc] init];
        shiShouLabel.font = SmallFont;
        shiShouLabel.text = @"实收：";
        shiShouLabel.textColor = CSColorFromRGB(0x7a7a7a);
        [bottomView addSubview:shiShouLabel];
        self.shiShouLabel = shiShouLabel;
        
        UILabel *moneyLabel = [[UILabel alloc] init];
        moneyLabel.font = MidFont;
        moneyLabel.text = @"5000元";
        moneyLabel.textColor = CSColorFromRGB(0x212121);
        [bottomView addSubview:moneyLabel];
        self.moneyLabel = moneyLabel;
        
        UIButton *sureBtn  = [[UIButton alloc] init];
        [sureBtn setTitle:@"确定"];
        sureBtn.backgroundColor = ButtonBackGroundColor;
        [sureBtn setTitleColor:CSColorFromRGB(0x212121)];
        [bottomView addSubview:sureBtn];
        self.sureBtn = sureBtn;
        
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.width.left.mas_equalTo(self);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
    [self.whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.scrollView);
    }];
    
    /**
     卡背景
     */
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.whiteBgView).offset(10);
        
        make.left.mas_equalTo(self.whiteBgView).offset(13);
        
        make.right.mas_equalTo(self).offset(-13);
        
        make.bottom.mas_equalTo(self.whiteBgView);
    }];
    
    /**
     卡名称
     */
    [self.cardClassNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.bgView).offset(20);
        
        make.left.mas_equalTo(self.bgView).offset(21);
    }];
    
    /**
     有效期
     */
    [self.validLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.cardClassNameLabel);
        
        make.top.mas_equalTo(self.cardClassNameLabel.mas_bottom).offset(15);
    }];
    
    /**
     会员ID
     */
    [self.vipNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.bgView).offset(-55);
        
        make.left.mas_equalTo(self.validLabel);
    }];
    
    /**
     会员卡号
     */
    [self.cardNumLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.vipNameLabel.mas_bottom).offset(5);
        
        make.left.mas_equalTo(self.vipNameLabel);
    }];
    
    /**
     余额
     */
    [self.balanceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.bgView).offset(-20);
        
        make.centerY.mas_equalTo(self.vipNameLabel);
    }];
    
    
    /**
     余额
     */
    [self.balanceDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.cardNumLabel);
        
        make.right.mas_equalTo(self.bgView).offset(-20);
    }];
    
    
    /**
     会员卡号
     */
    [self.vipCardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self);
        
        make.top.mas_equalTo(self.whiteBgView.mas_bottom).offset(15);
    }];
    
    /**
     销售
     */
    [self.salBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self);
        
        make.top.mas_equalTo(self.vipCardInputView.mas_bottom);
    }];
    
    /**
     介绍人
     */
    [self.introduceBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self);
        
        make.top.mas_equalTo(self.salBtnView.mas_bottom);
    }];
    
    /**
     对接教练
     */
    [self.coachBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self);
        
        make.top.mas_equalTo(self.introduceBtnView.mas_bottom);
    }];
    
    /**
     备注
     */
    [self.remarkBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.width.mas_equalTo(self.scrollView);
        
        make.top.mas_equalTo(self.coachBtnView.mas_bottom);
    }];
    
    /**
     支付
     */
    [self.payBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.width.mas_equalTo(self.scrollView);

        make.top.mas_equalTo(self.remarkBtnView.mas_bottom).offset(10);
    }];


    /**
     历史订单
     */
    [self.hisSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.width.mas_equalTo(self);

        make.top.mas_equalTo(self.payBtnView.mas_bottom).offset(10);
    }];


    [self.hisLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.scrollView).offset(10);

        make.right.mas_equalTo(self).offset(-10);
        
        make.top.mas_equalTo(self.hisSwitchView.mas_bottom).offset(10);
    }];

    [self.hisBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.width.mas_equalTo(self.scrollView);

        make.top.mas_equalTo(self.hisLabel.mas_bottom).offset(10);
        
    }];
 
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self);
        make.left.width.mas_equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    [self.shiShouLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.bottomView).offset(10);
        make.centerY.mas_equalTo(self.bottomView);
    }];
    
    [self.moneyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.shiShouLabel.mas_right).offset(1);
        make.centerY.mas_equalTo(self.bottomView);
    }];
    
    
    [self.sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.right.bottom.mas_equalTo(self.bottomView);
        make.width.mas_equalTo(self).multipliedBy(1.0 / 3);
    }];
    
}


-(void)layoutSubviews{
    
    if (self.hisSwitchView.titleSwitch.isOn) {
        
        self.scrollView.contentSize = CGSizeMake(self.width, self.hisBtnView.bottom + 10 + adaptNavagationBar);
        
    }
    else{
        
        self.scrollView.contentSize = CGSizeMake(self.width, self.hisLabel.bottom + 10 + adaptNavagationBar);
    }
}

-(void)switchAction:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        CommonBtnView *hisBtnView = [[CommonBtnView alloc] init];
        hisBtnView.titleLabel.text = @"历史日期";
        hisBtnView.choseLabel.text = @"请选择";
        hisBtnView.choseLabel.textColor = PlaceHoldColor;
        [hisBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self.scrollView addSubview:hisBtnView];
        self.hisBtnView = hisBtnView;
    }
    else{
        
        [self.hisBtnView removeFromSuperview];
        self.hisBtnView = nil;
        self.hisTime = @"";
    }
    
    [self setConstraints];
    
}

-(void)setSelectRow:(NSInteger)selectRow{
    
    self.bgView.image = [UIImage imageNamed:self.bgNameArray[selectRow % 3]];
    
    //黑色
    if (selectRow % 3 == 0) {
        
        
        /**
         卡名称
         */
        self.cardClassNameLabel.textColor = CSColorFromRGB(0xffffff);
        
        /**
         有效期
         */
        self.validLabel.textColor = CSColorFromRGB(0x97969c);
        
        /**
         会员ID
         */
        self.vipNameLabel.textColor = CSColorFromRGB(0xffffff);
        
        /**
         限制
         */
        
        self.cardNumLabel.textColor = CSColorFromRGBA(0x7b788d,0.7);
        
        /**
         余额
         */
        
        self.balanceLabel.textColor = CSColorFromRGB(0x97969c);
        
        
        /**
         余额
         */
        self.balanceDetailLabel.textColor = CSColorFromRGB(0xffffff);
        
    }
    else{
        
        /**
         卡名称
         */
        self.cardClassNameLabel.textColor = CSColorFromRGB(0x0a0a0a);
        
        /**
         有效期
         */
        self.validLabel.textColor = CSColorFromRGB(0x7d7c7f);
        
        /**
         会员ID
         */
        self.vipNameLabel.textColor = CSColorFromRGB(0x0a0a0a);
        
        
        /**
         限制
         */
        
        self.cardNumLabel.textColor = CSColorFromRGBA(0x000000,0.5);
        
        /**
         余额
         */
        
        self.balanceLabel.textColor = CSColorFromRGB(0x7d7c7f);
        
        
        /**
         余额
         */
        self.balanceDetailLabel.textColor = CSColorFromRGB(0x0a0a0a);
    }
}

-(void)beginTimeBtnClick:(UIButton *)btn{
    
    [self endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:114 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
        
        weakSelf.hisBtnView.choseLabel.text = [dateFormatter stringFromDate:date];
        
        weakSelf.hisBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.hisTime = [dateFormatter stringFromDate:date];
        
    } cancelBlock:^{
        
        
    }];
}

@end
